<script type="text/javascript" src="<?=base_url()?>js/popup.js"></script>
<link rel="stylesheet" type="text/css" href="<?=base_url()?>css/popup.css" />

<script>
$j(document).ready(function(){
	$j("#background-popup").click(function(){ disablePopup(); });
	$j(document).keypress(function(e){ if(e.keyCode==27 && popupStatus==1){ disablePopup(); } });
});
function addproduct() {
	window.location = '<?=base_url()?>admin/store/product/add';
}
function deleteproduct(id) {
	if (confirm('Are you sure you want to delete this product?')) {
		window.location = '<?=base_url()?>admin/store/deleteproduct/' + id;
	}
}
function switchstatus(id) {
	$j.ajax({
		url: '<?=base_url()?>admin/store/switchstatus/',
		type: 'POST',
		data: ({id:id}),
		dataType: "html",
		success: function(html) {
			$j('#active-' + id).html(html);
		}
	})
}
function category(product_id) {
day = new Date();
id = day.getTime();
URL = '<?=base_url()?>admin/store/productcategories/' + product_id;
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=500,left = 240,top = 125');");
}
function gallery(product_id) {
day = new Date();
id = day.getTime();
URL = '<?=base_url()?>admin/store/productgallery/' + product_id;
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=825,height=600,left = 240,top = 125');");
}
function crosssale(product_id) {
day = new Date();
id = day.getTime();
URL = '<?=base_url()?>admin/store/productcrosssale/' + product_id;
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=640,height=700,left = 240,top = 125');");
}
function sale(product_id) {
	$j.ajax({
		url: '<?=base_url()?>admin/store/getsale/',
		type: 'POST',
		data: ({product_id:product_id,current_url:'<?=current_url()?>'}),
		dataType: "html",
		success: function(html) {
			$j('#sale-content').html(html);
			centerPopup();
			loadPopup();
		}
	})
}
</script>
<div id="popup-box">
	<div id="sale-content">
    	
    </div>
</div>
<div id="background-popup"></div>
    	<div class="left">
        	<h1>Store Management</h1>
            <div class="bar">

            	<div class="text">Manage Products</div>
            	<div class="cr"></div>
            </div>
            <div class="box">
            	<p>
                	<input type="button" class="button rounded" value="Add new product" onclick="addproduct()" />
                    <input type="button" class="button rounded" value="Export products" />
                </p>
            </div>            
            <hr />
            <div class="box bgw">
            	<form method="post" action="<?=base_url()?>admin/store/searchproduct">
                <h3>Search Product</h3>
                <dl class="one"><dt>Keyword</dt>
                	<dd><input type="text" class="textfield rounded" name="keyword" /></dd></dl>
                <dl class="one"><dt>Category</dt><dd id="position">
				
                	<select name="category">
                    	<option value="0">All categories</option>						
                        <?php foreach($main as $maincat) { ?>
                        <?php $sub = $this->Category_model->get($maincat['id']);
							if(count($sub)>0){
							foreach($sub as $subcat) { ?>
                            <option value="<?=$subcat['id']?>"><?=$maincat['title']?> &raquo; <?=$subcat['title']?></option>
                            <?php 
							}
							}else{ ?>
						    <option value="<?=$maincat['id']?>"><?=$maincat['title']?></option>                    
                         	<?php }
							} 
						 ?>
                    </select>
                </dd><dd><input type="submit" class="button rounded" value="Search" /></dd></dl>
                <dl></dl>
                </form>
            </div>
            <hr />
            <div class="box">
            	<h3 style="float:left;">Products List</h3>
                <?php if($links) { ?>
                <div class="links"><?=$links?> | 10 Products per page</div>
                <?php } ?>
                <div class="row-title">
                	<div class="cat-name">Product name</div>					
                    <div class="quick-func">Quick functions</div>
					<div class="title-num">Stock</div>
                </div>
                <div id="subcat">
                	<?php foreach($products as $product) { ?>
                    <div class="row-item">
                    	<div class="cat-name"><a href="<?=base_url()?>admin/store/product/edit/<?=$product['id']?>"><?=$product['title']?></a></div>						
                        <div class="cat-func"><a href="javascript:deleteproduct(<?=$product['id']?>)" title="Delete this product"><img src="<?=base_url()?>img/backend/icon-delete.png" /></a></div>
                        <div class="cat-func" id="active-<?=$product['id']?>">
                        	<?php if ($product['status'] == 0) { ?>
                        	<a href="javascript:switchstatus(<?=$product['id']?>)" title="Active this prodcut"><img src="<?=base_url()?>img/backend/icon-inactived.png" /></a>
                            <?php } else if ($product['status'] == 1) { ?>
                            <a href="javascript:switchstatus(<?=$product['id']?>)" title="De-active this product"><img src="<?=base_url()?>img/backend/icon-actived.png" /></a>
                            <?php } ?>
                        </div>
                        <div class="cat-func"><a href="#" onclick="sale(<?=$product['id']?>)"><img src="<?=base_url()?>img/backend/icon-sale<?php if($product['price'] == $product['sale_price']) print "-off"; ?>.png" /></a></div>
                        <div class="cat-func"><a href="javascript:gallery(<?=$product['id']?>)" title="Gallery"><img src="<?=base_url()?>img/backend/icon-image.png" /></a></div>
                        <div class="cat-func"><a href="javascript:category(<?=$product['id']?>)" title="Categories"><img src="<?=base_url()?>img/backend/icon-folder.png" /></a></div>
                        <div class="cat-func"><a href="javascript:crosssale(<?=$product['id']?>)" title="Cross sale items"><img src="<?=base_url()?>img/backend/icon-clip.png" /></a></div>
						<!--<div class="title-num"><?=$product['stock']?></div>-->
						<div class="title-num">
							<?
							if($product['multiplesize']==0){ echo $product['stock']; }
							else {
								// count multiple size
								$multiple_stock = json_decode($product['size'],true);
								$stock_all= $multiple_stock['XXXS']+$multiple_stock['XS']+$multiple_stock['M']+$multiple_stock['XL']+$multiple_stock['3XL']+$multiple_stock['5XL']+$multiple_stock['6'];
								$stock_all=$stock_all+$multiple_stock['10']+$multiple_stock['14']+$multiple_stock['18']+$multiple_stock['22']+$multiple_stock['XXS']+$multiple_stock['S']+$multiple_stock['L'];
								$stock_all=$stock_all+$multiple_stock['XXL']+$multiple_stock['4XL']+$multiple_stock['6XL']+$multiple_stock['8']+$multiple_stock['12']+$multiple_stock['16']+$multiple_stock['20'];
								echo $stock_all;
							}
							
							?>
							
						</div>
                    </div>
                    <?php } ?>
                </div>
            </div>
        </div>
        
